package com.swanclouds.redfish.account.domain;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import com.swanclouds.model.account.LdapController;
import com.swanclouds.model.account.LdapGroup;
import com.swanclouds.model.account.builder.LdapControllerBuilder;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 张立强
 * @version jdk1.8.0
 */
public class LdapControllerConcrete implements LdapController {
    @JsonProperty("@odata.id")
    private String dataId;

    @JsonProperty("@odata.context")
    private String context;

    @JsonProperty("@odata.type")
    private String type;

    @JsonProperty("Id")
    private String id;

    @JsonProperty("Name")
    private String name;
    /**
     * 系统资源的访问路径
     *
     * @return dataId
     */
    @Override
    public String getDataId() {
        return this.dataId;
    }

    /**
     * 系统资源模型的OData描述信息
     *
     * @return context
     */
    @Override
    public String getContext() {
        return this.context;
    }

    /**
     * 系统资源的类型
     *
     * @return type
     */
    @Override
    public String getType() {
        return this.type;
    }

    /**
     * 系统资源的ID
     *
     * @return Id
     */
    @Override
    public String getId() {
        return this.id;
    }

    /**
     * 系统资源的名称
     *
     * @return Name
     */
    @Override
    public String getName() {
        return this.name;
    }

    @JsonProperty("LdapServerAddress")
    private String ldapServerAddress;

    @JsonProperty("LdapPort")
    private Integer ldapPort;

    @JsonProperty("UserDomain")
    private String userDomain;

    @JsonProperty("BindDN")
    private String bindDN;
    @JsonProperty("BindPassword")
    private String bindPassword;

    @JsonProperty("CertificateVerificationEnabled")
    private Boolean certificateVerificationEnabled;
    @JsonProperty("CrlVerificationEabled")
    private Boolean crlVerificationEabled;

    @JsonProperty("CrlValidFrom")
    private String crlValidFrom;
    @JsonProperty("CrlValidTo")
    private String crlValidTo;

    @JsonProperty("CertificateVerificationLevel")
    private String crtificateVerificationLevel;

    @JsonProperty("CertificateInformation")
    private LdapCert certificateInformation;

    @JsonProperty("CertificateChainInformation")
    private LdapServerCert certificateChainInformation;

    @JsonProperty("IntermediateCert")
    private List<LdapCert> intermediateCert;

    @JsonProperty("RootCert")
    private LdapCert rootCert;

    @JsonProperty("LdapGroups")
    private List<LdapGroupConcrete> ldapGroups;

    @JsonProperty("Actions")
    private Map<String,Object> actions;

    /**
     * 域控制器的地址
     *
     * @return this Ldap Controller address
     */
    @Override
    public String getLdapServerAddress() {
        return this.ldapServerAddress;
    }

    /**
     * 域控制器的端口号
     *
     * @return this Ldap Controller port
     */
    @Override
    public Integer getLdapPort() {
        return this.ldapPort;
    }

    /**
     * 域控制器的用户域
     *
     * @return this Ldap Controller domain
     */
    @Override
    public String getUserDomain() {
        return this.userDomain;
    }

    /**
     * Ldap代理用户标识名
     *
     * @return this Ldap Controller user sign
     */
    @Override
    public String getBindDN() {
        return this.bindDN;
    }

    /**
     * Ldap代理用户的认证密码
     *
     * @return this Ldap Controller user password
     */
    @Override
    public String getBindPassword() {
        return this.bindPassword;
    }

    /**
     * 证书启用的使能
     *
     * @return this Certificate enabled
     */
    @Override
    public Boolean getCertificateVerificationEnabled() {
        return this.certificateVerificationEnabled;
    }

    /**
     * 证书吊销检查使能
     *
     * @return this verification enabled
     */
    @Override
    public Boolean getCrlVerificationEabled() {
        return this.crlVerificationEabled;
    }

    /**
     * 证书吊销列表有效期起始日期
     *
     * @return this from
     */
    @Override
    public String getCrlValidFrom() {
        return this.crlValidFrom;
    }

    /**
     * 证书吊销列表有效期截止日期
     *
     * @return this to
     */
    @Override
    public String getCrlValidTo() {
        return this.crlValidTo;
    }

    /**
     * 证书校验级别,可选值["Demand","Allow"],仅在证书启用使能状态下生效
     *
     * @return this certificate Level
     */
    @Override
    public String getCertificateVerificationLevel() {
        return this.crtificateVerificationLevel;
    }

    /**
     * 证书信息
     *
     * @return this Certification information
     */
    @Override
    public Cert getCertificateInformation() {
        return this.certificateInformation;
    }

    /**
     * 证书链信息
     *
     * @return the server cert
     */
    @Override
    public ServerCert getCertificateChainInformation() {
        return this.certificateChainInformation;
    }

    /**
     * 中间证书
     *
     * @return the intermediate Certs
     */
    @Override
    public List<Cert> getIntermediateCert() {
        if (this.intermediateCert == null) {
            return Lists.newArrayList();
        }
        return this.intermediateCert.stream().collect(Collectors.toList());
    }

    /**
     * 根证书
     *
     * @return the root Cert
     */
    @Override
    public Cert getRootCert() {
        return this.rootCert;
    }

    /**
     * Ldap用户组
     *
     * @return the Ldap Groups
     */
    @Override
    public List<LdapGroup> getLdapGroups() {
        if (this.ldapGroups == null) {
            return Lists.newArrayList();
        }
        return this.ldapGroups.stream().collect(Collectors.toList());
    }

    /**
     * @return Map
     */
    @Override
    public Map<String, Object> getActions() {
        return this.actions;
    }

    @Override
    public LdapControllerBuilder toBuilder() {
        return new LdapControllerConcreteBuilder();
    }

    public static LdapControllerBuilder builder() {
        return new LdapControllerConcrete().toBuilder();
    }

    public static class LdapServerCert implements ServerCert {

        @JsonProperty("ServerCert")
        private Cert serverCert;

        /**
         * 服务器证书
         *
         * @return the server cert
         */
        @Override
        public Cert getServerCert() {
            return this.serverCert;
        }

        @Override
        public String toString() {
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                return objectMapper.writeValueAsString(this);
            } catch (JsonProcessingException e) {
            }
            return "LdapServerCert {" +
                    "serverCert=" + serverCert +
                    '}';
        }
    }
    public static class LdapCert implements Cert {

        @JsonProperty("IssueBy")
        private String issueBy;

        @JsonProperty("IssueTo")
        private String issueTo;

        @JsonProperty("ValidFrom")
        private String validFrom;

        @JsonProperty("ValidTo")
        private String validTo;

        @JsonProperty("SerialNumber")
        private String serialNumber;

        @JsonProperty("SignatureAlgorithm")
        private String signatureAlgorithm;

        @JsonProperty("KeyUsage")
        private String keyUsage;

        @JsonProperty("PublicKeyLengthBits")
        private Integer publicKeyLengthBits;

        /**
         * 证书的颁发者
         *
         * @return issue by
         */
        @Override
        public String getIssueBy() {
            return this.issueBy;
        }

        /**
         * 证书的使用者
         *
         * @return issue to
         */
        @Override
        public String getIssueTo() {
            return this.issueTo;
        }

        /**
         * 证书的开始时间
         *
         * @return this from time
         */
        @Override
        public String getValidFrom() {
            return this.validFrom;
        }

        /**
         * 证书的结束时间
         *
         * @return this to time
         */
        @Override
        public String getValidTo() {
            return this.validTo;
        }

        /**
         * 证书的序列号
         *
         * @return the cert SerialNumber
         */
        @Override
        public String getSerialNumber() {
            return this.serialNumber;
        }

        /**
         * 证书的签名算法
         *
         * @return this cert SignatureAlgorithm
         */
        @Override
        public String getSignatureAlgorithm() {
            return this.signatureAlgorithm;
        }

        /**
         * 证书的使用方法
         *
         * @return the cert usage
         */
        @Override
        public String getKeyUsage() {
            return this.keyUsage;
        }

        /**
         * 证书的公钥长度
         *
         * @return the key length
         */
        @Override
        public Integer getPublicKeyLengthBits() {
            return this.publicKeyLengthBits;
        }

        @Override
        public String toString() {
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                return objectMapper.writeValueAsString(this);
            } catch (JsonProcessingException e) {
            }
            return "LdapCert {" +
                    "issueBy='" + issueBy + '\'' +
                    ", issueTo='" + issueTo + '\'' +
                    ", validFrom='" + validFrom + '\'' +
                    ", validTo='" + validTo + '\'' +
                    ", serialNumber='" + serialNumber + '\'' +
                    ", signatureAlgorithm='" + signatureAlgorithm + '\'' +
                    ", keyUsage='" + keyUsage + '\'' +
                    ", publicKeyLengthBits=" + publicKeyLengthBits +
                    '}';
        }
    }

    public static class LdapControllerConcreteBuilder implements LdapControllerBuilder {
        private LdapControllerConcrete m;

        public LdapControllerConcreteBuilder() {
            this(new LdapControllerConcrete());
        }

        public LdapControllerConcreteBuilder(LdapControllerConcrete m) {
            this.m = m;
        }

        @Override
        public LdapControllerBuilder LdapServerAddress(String address) {
            m.ldapServerAddress = address;
            return this;
        }

        @Override
        public LdapControllerBuilder LdapPort(Integer port) {
            m.ldapPort = port;
            return this;
        }

        @Override
        public LdapControllerBuilder UserDomain(String userDomain) {
            m.userDomain = userDomain;
            return this;
        }

        @Override
        public LdapControllerBuilder BindDN(String bindDN) {
            m.bindDN = bindDN;
            return this;
        }

        @Override
        public LdapControllerBuilder BindPassword(String bindPassword) {
            m.bindPassword = bindPassword;
            return this;
        }

        @Override
        public LdapControllerBuilder CertificateVerificationEnabled(Boolean certificateVerificationEnabled) {
            m.certificateVerificationEnabled = certificateVerificationEnabled;
            return this;
        }

        @Override
        public LdapControllerBuilder CertificateVerificationLevel(String certificateVerificationLevel) {
            m.crtificateVerificationLevel = certificateVerificationLevel;
            return this;
        }

        @Override
        public LdapControllerBuilder LdapGroups(List<LdapGroup> ldapGroups) {
            if (m.ldapGroups == null) {
                m.ldapGroups = Lists.newArrayList();
            }
            if (ldapGroups == null) {
                return this;
            }
            ldapGroups.stream().forEach(group -> {
                m.ldapGroups.add((LdapGroupConcrete) group);
            });
            return this;
        }

        @Override
        public LdapControllerBuilder AddLdapGroup(LdapGroup ldapGroup) {
            if (m.ldapGroups == null) {
                m.ldapGroups = Lists.newArrayList();
            }
            if (ldapGroup == null) {
                return this;
            }
            m.ldapGroups.add((LdapGroupConcrete) ldapGroup);
            return this;
        }

        /**
         * Creates and return the Model Entity M
         *
         * @return M instance
         */
        @Override
        public LdapController build() {
            return m;
        }

        /**
         * Creates a Builder using the param M as the default values
         *
         * @param in The Model M
         * @return Builder
         */
        @Override
        public LdapControllerBuilder from(LdapController in) {
            m = (LdapControllerConcrete) in;
            return this;
        }

        @Override
        public String toString() {
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                return objectMapper.writeValueAsString(this);
            } catch (JsonProcessingException e) {
            }
            return "LdapControllerConcreteBuilder {" +
                    "m=" + m +
                    '}';
        }
    }

    @Override
    public String toString() {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.writeValueAsString(this);
        } catch (JsonProcessingException e) {
        }
        return "LdapControllerConcrete {" +
                "dataId='" + dataId + '\'' +
                ", context='" + context + '\'' +
                ", type='" + type + '\'' +
                ", id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", ldapServerAddress='" + ldapServerAddress + '\'' +
                ", ldapPort=" + ldapPort +
                ", userDomain='" + userDomain + '\'' +
                ", bindDN='" + bindDN + '\'' +
                ", bindPassword='" + bindPassword + '\'' +
                ", certificateVerificationEnabled=" + certificateVerificationEnabled +
                ", crlVerificationEabled=" + crlVerificationEabled +
                ", crlValidFrom='" + crlValidFrom + '\'' +
                ", crlValidTo='" + crlValidTo + '\'' +
                ", crtificateVerificationLevel='" + crtificateVerificationLevel + '\'' +
                ", certificateInformation=" + certificateInformation +
                ", certificateChainInformation=" + certificateChainInformation +
                ", intermediateCert=" + intermediateCert +
                ", rootCert=" + rootCert +
                ", ldapGroups=" + ldapGroups +
                ", actions=" + actions +
                '}';
    }
}
